﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WikiTrailCreator.TrailImages;
using WikiTrailCreator.SpecialDialogs;

namespace WikiTrailCreator.MultiTextbox
{
    public partial class ucMultiTextbox : UserControl
    {
        private BindingList<MultiTextboxItem> listItems = new BindingList<MultiTextboxItem>();
        private bool bUseListItemData = true;

        public ucMultiTextbox()
        {
            InitializeComponent();

            listBox1.DataSource = listItems;
            listBox1.DisplayMember = "ItemName";
            listBox1.ValueMember = "ItemName";
        }

        private void ucMultiTextbox_Load(object sender, EventArgs e)
        {

        }

        [Browsable(true)]
        public bool bIsListItemDataMultiline
        {
            get { return textBoxListItemData.Multiline; }
            set { textBoxListItemData.Multiline = value; }
        }

        [Browsable(true)]
        public string ListItemLabelText
        {
            get { return labelListItem.Text;  }
            set { labelListItem.Text = value; }
        }

        [Browsable(true)]
        public string ListItemDataLabelText
        {
            get { return labelListItemData.Text; }
            set { labelListItemData.Text = value; }
        }

        [Browsable(true)]
        public bool bShowListItemData
        {
            get { return bUseListItemData; }
            set 
            { 
                bUseListItemData = value;

                textBoxListItemData.Visible = bUseListItemData;
                labelListItemData.Visible = bUseListItemData;
            }
        }

        [Browsable(true)]
        public string ListTitle
        {
            get { return labelTitle.Text; }
            set { labelTitle.Text = value; }
        }

        public MultiTextboxItem[] Items
        {
            get 
            { 
                return listItems.ToArray();  
            }
            set
            {
                listItems.Clear();

                if (value != null && value.Length > 0)
                {
                    foreach (MultiTextboxItem item in value)
                        listItems.Add(item);

                    listBox1.SelectedItem = value[0];
                }

                listBox1_SelectedIndexChanged(this, new EventArgs());
            }
        }

        private void addToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.ValidateChildren();

            MultiTextboxItem newItemWithText = new MultiTextboxItem("New Item");
            listItems.Add(newItemWithText);

            listBox1.SelectedItem = newItemWithText;
            listBox1_SelectedIndexChanged(this, new EventArgs());
        }

        private void contextMenuStripListbox_Opening(object sender, CancelEventArgs e)
        {
            this.ValidateChildren();

            deleteToolStripMenuItem.Enabled = (listBox1.SelectedItem != null);
        }

        private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (listBox1.SelectedItem != null)
            {
                listItems.Remove((MultiTextboxItem) listBox1.SelectedItem);
            }
        }

        private void textBoxListItem_Validated(object sender, EventArgs e)
        {
            MultiTextboxItem selectedItem = ((MultiTextboxItem)listBox1.SelectedItem);

            if (selectedItem != null)
            {
                selectedItem.ItemName = textBoxListItem.Text;
                listItems.ResetItem(listItems.IndexOf(selectedItem));
            }
        }

        private void textBoxListItemData_Validated(object sender, EventArgs e)
        {
            MultiTextboxItem selectedItem = ((MultiTextboxItem)listBox1.SelectedItem);

            if (selectedItem != null)
            {
                selectedItem.ItemText = textBoxListItemData.Text;
            }
        }

        private void listBox1_MouseDown(object sender, MouseEventArgs e)
        {

        }

        private void buttonMoveUp_Click(object sender, EventArgs e)
        {
            MultiTextboxItem selectedItem = ((MultiTextboxItem)listBox1.SelectedItem);

            if (selectedItem != null)
            {
                int indexOfSelected = listItems.IndexOf(selectedItem);

                if (indexOfSelected > 0)
                {
                    listItems.Remove(selectedItem);
                    listItems.Insert(indexOfSelected - 1, selectedItem);
                    listBox1.SelectedItem = selectedItem;
                }
            }
        }

        private void buttonMoveDown_Click(object sender, EventArgs e)
        {
            MultiTextboxItem selectedItem = ((MultiTextboxItem)listBox1.SelectedItem);

            if (selectedItem != null)
            {
                int indexOfSelected = listItems.IndexOf(selectedItem);

                if (indexOfSelected < listItems.Count - 1)
                {
                    listItems.Remove(selectedItem);
                    listItems.Insert(indexOfSelected + 1, selectedItem);
                    listBox1.SelectedItem = selectedItem;
                }
            }
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            MultiTextboxItem selectedItem = ((MultiTextboxItem)listBox1.SelectedItem);

            if (selectedItem != null)
            {
                textBoxListItem.Text = selectedItem.ItemName;
                textBoxListItemData.Text = selectedItem.ItemText;
                groupBoxDetails.Enabled = true;
                buttonMoveDown.Enabled = true;
                buttonMoveUp.Enabled = true;
            }
            else
            {
                groupBoxDetails.Enabled = false;
                buttonMoveDown.Enabled = false;
                buttonMoveUp.Enabled = false;
            }
        }

        private void groupBoxDetails_Enter(object sender, EventArgs e)
        {

        }

        private void insertThumbnailToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormImageSelector formImageSelector = new FormImageSelector();
            DialogResult dialogResult = formImageSelector.ShowDialog();

            if (dialogResult == DialogResult.OK)
            {
                string wikiCode = formImageSelector.GetInternalThumbnailCode();
                textBoxListItemData.Text = textBoxListItemData.Text.Insert(textBoxListItemData.SelectionStart, wikiCode); 
            }

            formImageSelector.Dispose();
        }

        private void insertGalleryToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormGalleryCreator formGalleryCreator = new FormGalleryCreator();
            DialogResult dialogResult = formGalleryCreator.ShowDialog();

            if (dialogResult == DialogResult.OK)
            {
                string internalCode = formGalleryCreator.GetInternalGalleryCode();
                textBoxListItemData.Text = textBoxListItemData.Text.Insert(textBoxListItemData.SelectionStart, internalCode);
            }

            formGalleryCreator.Dispose();
        }

        private void insertLinkToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormAddLink formAddLink = new FormAddLink();
            DialogResult dialogResult = formAddLink.ShowDialog();

            if (dialogResult == DialogResult.OK)
            {
                string wikiCode = formAddLink.GetLinkAsWikiText();
                textBoxListItemData.Text = textBoxListItemData.Text.Insert(textBoxListItemData.SelectionStart, wikiCode);
            }

            formAddLink.Dispose();
        }
    }
}
